package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * banner表 后台控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-11
 */
@RestController
@RequestMapping("/educms/crmbanner")
//@CrossOrigin
public class BackgroundBannerController {

    @Autowired
    CrmBannerService crmBannerService;

    @ApiOperation(value = "分页获取")
    @GetMapping("/getBannerPage/{current}/{limit}")
    public R getBannerPage(@PathVariable long current, @PathVariable long limit){
        System.out.println("current"+current);
        Page<CrmBanner> bannerPage = new Page<>(current,limit);
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("createtime");
        crmBannerService.page(bannerPage,wrapper);
        List<CrmBanner> records = bannerPage.getRecords();
        long total = bannerPage.getTotal();
        return R.ok().data("list",records).data("total",total);
    }

    @ApiOperation(value = "添加轮播图")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "修改轮播图信息")
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "获取对应id的轮播图")
    @GetMapping("/getBannerId/{id}")
    public R getBannerId(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("crmBanner",crmBanner);
    }

    @ApiOperation(value = "删除对应id的轮播图")
    @DeleteMapping("/deleteBannerId/{id}")
    public R deleteBannerId(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }


}

